import controller.fileServiceController;
import controller.loginServiceController;
import controller.userServiceController;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ui.ConcurrentModel;
import org.springframework.ui.Model;
import pojo.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

//使用层
public class T {
    //模拟启动
    static loginServiceController loginServiceController;
    static userServiceController userServiceController;
    static fileServiceController fileServiceController;
    static {
        ApplicationContext context = new ClassPathXmlApplicationContext("springmvc-servlet.xml");
        loginServiceController = (loginServiceController) context.getBean("loginServiceController");
        userServiceController = (userServiceController) context.getBean("userServiceController");
        fileServiceController=(fileServiceController) context.getBean("fileServiceController");
    }
    @Test
    public void 获取用户界面() throws IOException {
        Model model=new ConcurrentModel();
        User user=new User(1);
        userServiceController.loadPersonalInformation(user,model);
        //根据是否是自身登入用户选择加载动态
        userServiceController.loadTheNewsOfInterest(user,model);
    }
    @Test
    public void test_login() {
        //模拟登入信息
        LoginInformation loginInformation = new LoginInformation("admin", "admin", "123456");
        //进行登入
        User user = loginServiceController.loginService.loginUser(loginInformation);
        //获取登入后的信息验证成功
        System.out.println(user.getUsername());
        System.out.println(user.getEmail());
        System.out.println(user.getTelephone());
        System.out.println(user.getId());
    }
    @Test
    public void test_register() {
        //模拟注册信息
        User newuser = new User();
        newuser.setUsername("admin2");
        newuser.setEmail("TorE");
        String password = "12345678";
        //进行注册
        Model model=new ConcurrentModel();
        loginServiceController.register(newuser.getUsername(),newuser.getEmail(),password,model);
        //获取注册自动登入的信息验证成功
        newuser=(User) model.getAttribute("User");
        System.out.println(newuser.getId());
        System.out.println(newuser.getUsername());
        System.out.println(newuser.getTelephone());
        System.out.println(newuser.getEmail());
    }
    @Test
    public void test_getback() {
        //模拟找回信息
        User user=new User();
        user.setEmail("@");
        //进行找回
        User rUser = loginServiceController.loginService.getbackUser(user);
        //验证找回
        if (rUser==null){
            System.out.println("error" + "该 用户 不存在");
            return;
        }
        //找回成功获得原密码，用户信息
        String userpassword = loginServiceController.loginService.getUserpassword(rUser.getId());
        System.out.println(rUser.getId());
        System.out.println(rUser.getUsername());
        System.out.println(rUser.getTelephone());
        System.out.println(rUser.getEmail());
        System.out.println(userpassword);
    }
    @Test
    public void test_getVerificationCode(){
        Model model=new ConcurrentModel();
        loginServiceController.getVerificationCode(model);
    }
    @Test
    public void test_CreateUserInformation() throws IOException {
        controller.userServiceController.createUserInformation(1);
    }
    @Test
    public void test_reviseUserLoginInformation(){
        userServiceController.reviseUserLoginInformation(1, UserInformationType.Password,"654321");
    }
    @Test
    public void test_viewUserInformation() throws IOException {
        Model model=new ConcurrentModel();
        userServiceController.myUser=new User(1);
        userServiceController.viewUserInformation(1,model);
        System.out.println(model.asMap().toString());
    }
    @Test
    public void test_ReviseUserSetInformation() throws IOException{
        User newuser=new User();
        newuser.setId(1);
        userServiceController.reviseUserSetInformation(newuser, UserSetInformationType.Following,2,false);
    }
    @Test
    public void test_createFile() throws IOException {
        Model model=new ConcurrentModel();
        User user=new User();
        user.setId(1);
        user.setUsername("admin");
        fileServiceController.createFile(user,"hello",model);
        Map<String, Object> map = model.asMap();
        FileInformation fileInformation=(FileInformation) map.get("FileInformation");
        System.out.println(fileInformation.getFileId());
        System.out.println(fileInformation.getFilename());
        System.out.println(fileInformation.getAuthorid());
        System.out.println(fileInformation.getAuthor());
    }
    @Test
    public void test_addEndLog() throws IOException {
//        controller.userServiceController.addEndLog(1,1,2,"hello");
        File path = new File("E:\\java\\Spring\\lightblog\\file/" + 1 + "/UserInformation/UserLogInformation.usi");
        LocalDate oldLocalDate=LocalDate.ofInstant(new Date(path.lastModified()).toInstant(), ZoneOffset.ofHours(8));
        System.out.println(oldLocalDate.format(DateTimeFormatter.ofPattern("uu-MM-dd")));
    }
}
